﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using CinemaBookingProject.DBLayer;
using System.Data;

public class DBBooking : DataBase
{
    public Booking read(string customerPhone)
    {
            SqlDataReader rdr = null;
            Booking booking = null;
            List<Ticket> tic = null;
            

            try
            {
                Connection.Open();

                string readQuery = "Select * from Booking where CustomerPhone LIKE @customerPhone;";
                SqlCommand cmd = new SqlCommand(readQuery, Connection);
                cmd.Parameters.Add("%@customerPhone%", SqlDbType.VarChar).Value = customerPhone;

                rdr = cmd.ExecuteReader();
                //løsning 1:
                //collect all ticket in a list men en sql query bagefter lave en sotering i et foreach loop. 

                //løsning 2
                // lave en sql query på alle tickets med booking id'et og kom dem i en liste

                tic = DBTicket.ReadAll((int)rdr["ID"]);

                while (rdr.Read())
                {
                    booking = new Booking((double) rdr["TotalPrice"],(string)rdr ["BookingStatus"],(string)rdr["CustomerPhone"], tic);
                }
            }
            finally
            {
                //close reader
                if (rdr != null)
                {
                    rdr.Close();
                }

                // close connection
                if (Connection != null)
                {
                    Connection.Close();
                }
            }

            return booking;
        }
    public Booking read(Customer customer)
    {
        SqlDataReader rdr = null;
        Booking booking = null;
        List<Ticket> tic = null;


        try
        {
            Connection.Open();

            string readQuery = "Select * from Booking where CustomerPhone LIKE @customerPhone;";
            SqlCommand cmd = new SqlCommand(readQuery, Connection);
            cmd.Parameters.Add("%@customerPhone%", SqlDbType.VarChar).Value = customer.PhoneNo;

            rdr = cmd.ExecuteReader();
            //løsning 1:
            //collect all ticket in a list men en sql query bagefter lave en sotering i et foreach loop. 

            //løsning 2
            // lave en sql query på alle tickets med booking id'et og kom dem i en liste

            tic = DBTicket.ReadAll((int)rdr["ID"]);

            while (rdr.Read())
            {
                booking = new Booking((double)rdr["TotalPrice"], (string)rdr["BookingStatus"], (string)rdr["CustomerPhone"], tic);
            }
        }
        finally
        {
            //close reader
            if (rdr != null)
            {
                rdr.Close();
            }

            // close connection
            if (Connection != null)
            {
                Connection.Close();
            }
        }

        return booking;
    }
































    //private SqlConnection con;

    //public Booking book
    //{
    //    get;
    //    set;
    //}

    //public Booking BuildBooking(DataRow dr)
    //{
    //    throw new System.NotImplementedException();
    //}

    //public List<Booking> GetAllBookings()
    //{
    //    throw new System.NotImplementedException();
    //}

    //public Booking SingleWhere(string query, SQLParameter[] parameter = null)
    //{
    //    throw new System.NotImplementedException();
    //}

    //public List<Booking> MiscWhere(string query, SQLParameter[] parameter = null)
    //{
    //    throw new System.NotImplementedException();
    //}

    //public Booking GetBookingByCustomerPhone(string phone)
    //{
    //    throw new System.NotImplementedException();
    //}

}

